package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.ProfessionalStuent;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.ProfessionalStuentQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IProfessionalStuentService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("professionalStuent")
public class ProfessionStuentController {
    /**
     * 正式学员
     */
    @Autowired
    private IProfessionalStuentService professionalStuentService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequestMapping("")
    @PermissionName("正式学员视图")
    @RequiresPermissions("professionalStuent:index")
    public String index(){
        return "professionalStuent/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @PermissionName("正式学员列表")
    @RequiresPermissions("professionalStuent:list")
    public PageResult list(ProfessionalStuentQueryObject qo){

        PageResult result = professionalStuentService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    @PermissionName("正式学员查询全部")
    @RequiresPermissions("professionalStuent:listAll")
    public List<ProfessionalStuent> listAll(){
        return professionalStuentService.selectAll();
    }

    /**
     * 查询一条数据
     * @return
     */
    @RequestMapping("selectOne")
    @ResponseBody
    @PermissionName("正式学员查询单个")
    @RequiresPermissions("professionalStuent:selectOne")
    public ProfessionalStuent selectOne(Long id){

        ProfessionalStuent ps = professionalStuentService.selectByPrimaryKey(id);

        return ps;
    }

    /**
     * 删除
     * @param professionalStuentId 要删除的正式学员对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("正式学员删除")
    @RequiresPermissions("professionalStuent:delete")
    public JsonResult delete(Long professionalStuentId){
        JsonResult result = new JsonResult();
        try{
            professionalStuentService.deleteByPrimaryKey(professionalStuentId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param professionalStuent 要更新的正式学员对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("正式学员更新")
    @RequiresPermissions("professionalStuent:update")
    public JsonResult update(ProfessionalStuent professionalStuent){
        JsonResult result = new JsonResult();
        try{
            professionalStuentService.updateByPrimaryKey(professionalStuent);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *
     *让休学学员
     *正式学员复学
     */
    @RequestMapping("goBack")
    @ResponseBody
    @PermissionName("正式学员复学")
    @RequiresPermissions("professionalStuent:goBack")
    public JsonResult goBack(Long id){
        JsonResult result = new JsonResult();
        try{
            ProfessionalStuent pS = professionalStuentService.selectByPrimaryKey(id);
            if(pS.getState() == ProfessionalStuent.STATE_QUIT ){
                pS.setState(ProfessionalStuent.STATE_READING);
                professionalStuentService.updateProfessionalStuentState(pS);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    /**
     *让同学休学
     *
     *
     */
    @RequestMapping("ouit")
    @ResponseBody
    @PermissionName("正式学员休学")
    @RequiresPermissions("professionalStuent:ouit")
    public JsonResult ouit(Long id){
        JsonResult result = new JsonResult();
        try{
            ProfessionalStuent pS = professionalStuentService.selectByPrimaryKey(id);
            if(pS.getState() == ProfessionalStuent.STATE_READING ||
                    pS.getState() == ProfessionalStuent.STATE_TRANSFER
                    ){
                pS.setState(ProfessionalStuent.STATE_QUIT);
                professionalStuentService.updateProfessionalStuentState(pS);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param professionalStuent 要保存的正式学员对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("正式学员保存")
    @RequiresPermissions("professionalStuent:save")
    public JsonResult save(ProfessionalStuent professionalStuent){
        JsonResult result = new JsonResult();
        try{
            professionalStuentService.insert(professionalStuent);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


}
